package com.usc.uscface.tools;



import com.usc.uscface.enums.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.usc.uscface.tools
 * @date 2020/11/16 20:47
 */

public class Face {
    /**
     * python web 的ip地址
     */
    private String ip_address;
    /**
     * python web 的host端口号
     */
    private String ip_host;

    RestTemplate restTemplate = new RestTemplate();

    public Face() {
    }

    public Face(String ip_address, String ip_host) {
        this.ip_address = ip_address;
        this.ip_host = ip_host;
    }

    /**
     * 文件入库
     * @param file 单个文件
     * @param target 库文件要存放的地址
     * @return 返回布尔值
     * @throws IOException
     */
    public String uploadFile(MultipartFile file, String target) throws IOException {
        String filePath = file.getOriginalFilename(); // 文件的全路径 filepath=2班/2020002/2020002_小红.jpg
        String[] split = filePath.split("/"); //
        String fileName = split[split.length-1];;// 图片名
        String newFilePath = "";
        for (int i =0 ;i<split.length-1;i++){
            newFilePath = newFilePath+"\\" +split[i];
        }
        newFilePath = target+""+newFilePath; //"\2班\2020002"
        File file1 = new File(newFilePath);
        if (!file1.exists()){
            file1.mkdirs();
        }
        String allFilePath = newFilePath +"\\"+fileName;// F:\Test\active\2班\2020002\2020002_小红.jpg
        File file2 = new File(allFilePath);
        if (!file2.exists()){
            file2.createNewFile();
        }

        file.transferTo(file2);
        return split[0];//返回图片的班级名

    }

    /**
     *  对库文件的图片进行编码
     * @param picture_target    库文件的地址
     * @param vector_targets    编码后库文件的存放地址
     * @return  返回处理结果
     */
    public CodeResponse FileCode(String picture_target, String vector_targets){
        String url = ip_address+':'+ip_host+"/upload_web?url="+picture_target+"&vector="+vector_targets;
        CodeResponse json = restTemplate.getForObject(url, CodeResponse.class);
        return json;
    }


    /**
     *
     * 图片临时存放的地址
     * @param file 要上传的图片
     * @param target    图片存放的地址
     * @return 返回图片临时存放的地址
     */
    public String getUpLoadUrl(MultipartFile file, String target) throws IOException {
        String filePath = file.getOriginalFilename(); // 文件的全路径
        String[] split = filePath.split("/");
        String fileName = split[split.length-1];;// 图片文件名
        String allFilePath = target +"\\"+fileName;
        File file2 = new File(allFilePath);
        if (!file2.exists()){
            file2.createNewFile();
        }
        file.transferTo(file2);
        return allFilePath;
    }

    /**
     * 对图片进行切割
     * @param file 图片
     * @param tempPath    图片临时存放的地址
     * @return  返回经过处理的json数据
     * @throws IOException
     */
    public CropResponses crop(MultipartFile file, String tempPath)throws IOException{
        String allPath = getUpLoadUrl(file, tempPath); // 先将图片进行存放
        String url = ip_address+':'+ip_host+"/crop_web?single_url="+allPath;
        CropResponses json = restTemplate.getForObject(url, CropResponses.class);//获取得到的数据
        return json;
    }




    /**
     *  人脸对比
     * @param file 图片
     * @param tempPath 图片要临时存放的地址
     * @param homeurl   编码后库文件的地址
     * @return  返回对比结果
     * @throws IOException
     */
    public Compare compare(MultipartFile file , String tempPath, String homeurl) throws IOException {
        String allPath = getUpLoadUrl(file,tempPath);
        String url = ip_address+':'+ip_host+"/compare_web?cpr_single_url="+allPath+"&home_url="+homeurl;
        CompareResponse json = restTemplate.getForObject(url, CompareResponse.class);

        Compare compare = new Compare();

        //处理比对
        List<List<CompareData>> data_list = new ArrayList<>();
        ArrayList<CompareData> compareData_list = new ArrayList<>();

        for (int i =0 ;i<json.getData().size();i++){
            CompareData compareData = new CompareData();
            for (int j = 0; j< json.getData().get(i).size();j++){
                compareData.setId(json.getData().get(i).get(0));
                compareData.setName(json.getData().get(i).get(1));
                compareData.setDifferenceDegree(Double.parseDouble(json.getData().get(i).get(2)));
            }
            compareData_list.add(compareData);
        }
        data_list.add(compareData_list);
        //封装
        compare.setData(data_list);

        if (data_list.size()!=0){
            compare.setCode(200);
            compare.setMessage("识别成功");
        }else{
            compare.setCode(100);
            compare.setMessage("识别失败");
        }
        return compare;
    }

    /**
     * 姿态识别
     * @param file  要进行识别的图片
     * @param tempPath  对要识别的图片的进行临时存储的地址
     * @return
     * @throws IOException
     */
    public Recognition recognition(MultipartFile file,String tempPath) throws IOException {
        String allPath = getUpLoadUrl(file,tempPath);
        String url = ip_address+':'+ip_host+"/action_recognition?single_url="+allPath;
        Recognition forObject = restTemplate.getForObject(url, Recognition.class);
        return forObject;
    }


    /**
     * 姿态识别 单张图片
     * @param file  要进行识别的图片
     * @param tempPath  对要识别的图片的进行临时存储的地址
     * @return
     * @throws IOException
     */
    public ActionRecognition actionRecognition(MultipartFile file ,String tempPath) throws IOException {
        String allPath = getUpLoadUrl(file,tempPath);
        String url = ip_address+':'+ip_host+"/action_recognition?single_url="+allPath;
        String now_url = "http://posegesture.ngrok2.xiaomiqiu.cn/action_recognition?single_url=/data/pose/data/test/*";
        ActionRecognition actionRecognition = restTemplate.getForObject(now_url, ActionRecognition.class);
        return actionRecognition;
    }

    /**
     * 姿态识别 对文件夹进行识别
     * @param activePath  姿态识别文件夹路径
     * @return
     * @throws IOException
     */
    public ActionRecognitions actionRecognitions(String activePath) throws IOException {
        String url = ip_address+':'+ip_host+"/action_recognition?single_url="+activePath;
        String now_url = "http://posegesture.ngrok2.xiaomiqiu.cn/action_recognition?single_url=/data/pose/data/test/*";
        ActionRecognitions actionRecognitions = restTemplate.getForObject(now_url, ActionRecognitions.class);
        return actionRecognitions;
    }


}
